package server.locations;


import java.util.ArrayList;
import java.util.List;

import server.players.Player;
import server.players.PlayerGroup;

public abstract class Location {
	protected List<PlayerGroup> playerGroups = null;
	
	public Location() {
		this.playerGroups = new ArrayList<PlayerGroup>();
	}
	
	abstract public void addPlayerGroup(PlayerGroup playerGroup);
	
	/**
	 * This method should only be called by other Location's addPlayerGroup
	 * methods. It safely removes the PlayerGroup from their current group.
	 * 
	 * @param playerGroup
	 */
	protected void removePlayerGroupFromCurrentLocation(PlayerGroup playerGroup)
	{
		if (playerGroup.getLocation() != null) {
			if (playerGroup.getLocation().hasPlayerGroup(playerGroup)) {
				playerGroup.getLocation().removePlayerGroup(playerGroup);
				// TODO Send packets to all other player groups,
				//		detailing the departure
			}
			// TODO	Throw exception.
		}
	}
	
	private void removePlayerGroup(PlayerGroup pg) {
		this.playerGroups.remove(pg);
	}
	
	public boolean hasPlayerGroup(PlayerGroup pg) {
		return this.playerGroups.contains(pg);
	}
	public boolean hasPlayer(Player p)
	{
		for (PlayerGroup pg : playerGroups) {
			if (pg.hasPlayer(p))
				return true;
		}
		return false;
	}
	
}
